layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 内容审核列表管理
     */
    var DbContentapproval = {
        tableId: "dbContentapprovalTable"
    };

    /**
     * 初始化表格的列
     */
    DbContentapproval.initColumn = function () {
        return [[
            {type:'numbers',title: '序号',width:100},
            {field: 'id', hide: true, title: 'id'},
            {field: 'serviceid', sort: true, title: '申请服务id', hide: true, align: 'center'},
            {field: 'content', sort: false, title: '申请内容', templet: function (res) {
                    if (res.content == '0') {
                        return '注册申请'
                    }
                    // else if (res.content == '1'){
                    //     return '发布申请'
                    // }
                    else if (res.content == '2'){
                        return '升级申请'
                    }else if (res.content == '3'){
                        return '下架申请'
                    }
                    // else if (res.content == '4'){
                    //     return '使用申请'
                    // }
                    else if (res.content == '5'){
                        return '激活申请'
                    }
                }},
            {field: 'userId', sort: true, title: '用户id', hide: true, align: 'center'},
            {field: 'department', sort: false, title: '单位'},
            {field: 'system', sort: false, title: '应用系统'},
            {field: 'servicetype', sort: false, title: '服务类型', templet: function (res) {
                    if (res.servicetype == '1') {
                        return '数据库服务'
                    }
                }},
            {field: 'standard', sort: false, title: '服务使用规范'},
            {field: 'details', sort: false, title: '说明'},
            {field: 'state', sort: false, title: '状态', templet: function (res) {
                    if (res.state == '0') {
                        return '待处理'
                    } else if(res.state == '1'){
                        return '已批准'
                    }
                    else if(res.state == '2'){
                        return '已拒绝'
                    }
                    else if(res.state == '3'){
                        return '升级中'
                    }
                    else if(res.state == '4'){
                        return '升级完成'
                    }
                }},
            {field: 'remarks', sort: false, title: '备注'},
            {field: 'revision', sort: true, title: '乐观锁', hide: true, align: 'center'},
            {field: 'createdBy', sort: true, title: '创建人', hide: true, align: 'center'},
            {field: 'createdTime', sort: true, title: '创建时间', hide: true, align: 'center'},
            {field: 'updatedBy', sort: true, title: '更新人', hide: true, align: 'center'},
            {field: 'updatedTime', sort: true, title: '更新时间', hide: true, align: 'center'},
            {align: 'center', toolbar: '#tableBar', title: '操作',field: 'state',width:150}
        ]];
    };

    /**
     * 点击查询按钮
     */
    DbContentapproval.search = function () {
        var queryData = {};
        queryData['department'] = $('#department').val();
        queryData['servicetype'] = $('#servicetype').val();
        queryData['system'] = $('#system').val();
        queryData['state'] = $('#state').val();
        queryData['content'] = $('#checkContent input[name="content"]:checked ').val();

        table.reload(DbContentapproval.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    DbContentapproval.openAddDlg = function () {
        func.open({
            title: '添加内容审核列表',
            content: Feng.ctxPath + '/dbContentapproval/add',
            tableId: DbContentapproval.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      DbContentapproval.openEditDlg = function (data) {
          func.open({
              title: '修改内容审核列表',
              content: Feng.ctxPath + '/dbContentapproval/edit?id=' + data.id,
              tableId: DbContentapproval.tableId
          });
      };

    /**
     * 批准按钮
     * @param data
     */
    DbContentapproval.onApprove = function (data){
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dbContentapproval/onApprove", function (data) {
                Feng.success("批准成功!");

                table.reload(DbContentapproval.tableId);
            }, function (data) {
                Feng.error("批准失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.set("serviceid",data.serviceid);
            ajax.set("content",data.content);
            ajax.start();
        };
        Feng.confirm("是否批准?", operation);
    };
    /**
     * 驳回按钮
     * @param data
     */
    DbContentapproval.onDown = function (data) {
        console.log(data)
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/dbContentapproval/onDown", function (data) {
                Feng.success("驳回成功!");

                table.reload(DbContentapproval.tableId);
            }, function (data) {
                Feng.error("驳回失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.set("serviceid",data.serviceid);
            ajax.start();
        };
        Feng.confirm("是否驳回?", operation);
    }
    // 渲染表格
    var tableResult = table.render({
        elem: '#' + DbContentapproval.tableId,
        url: Feng.ctxPath + '/dbContentapproval/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        skin: 'line',
        cols: DbContentapproval.initColumn(),
        done: function () {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {

        DbContentapproval.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    DbContentapproval.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        DbContentapproval.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + DbContentapproval.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            DbContentapproval.openEditDlg(data);
        } else if (layEvent === 'delete') {
            DbContentapproval.onDeleteItem(data);
        }else if (layEvent === 'Approve'){
            DbContentapproval.onApprove(data);
        }else if (layEvent === 'down'){
            DbContentapproval.onDown(data);
        }
    });
});
